﻿using System;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Windows;

namespace DetectApp;

/// <summary>
/// SystemSetView.xaml 的交互逻辑
/// </summary>
public partial class SystemSetView : Window
{
    public string filePath = "systemdata.json";
    private systemData _systemData;

    public SystemSetView()
    {
        InitializeComponent();
       var systemSetViewModel = new SystemSetViewModel();
        DataContext =  systemSetViewModel;
        if (File.Exists(filePath))
        {
            using (var filestream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                _systemData = JsonSerializer.Deserialize<systemData>(filestream) ?? new();
            }
        }
        else
        {
            _systemData = new systemData();
        }

       
    }

    private void ChangPass_Click(object sender, RoutedEventArgs e)
    {
        if (PbOld.Password == _systemData.PassWord && PbNew1.Password == PbNew2.Password)
        {
            _systemData.PassWord = PbNew1.Password;
            MessageBox.Show("修改成功");
        }
        else
        {
            MessageBox.Show("密码错误");
        }
    }

    private void Window_Closed(object sender, EventArgs e)
    {
        if (File.Exists(filePath)) { File.Delete(filePath); }
        using (var filestream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
        {
            JsonSerializer.Serialize(filestream, _systemData, new JsonSerializerOptions
            {
                WriteIndented = true,
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
            });
        }
    }

    //private void SelectPath_Click(object sender, RoutedEventArgs e)
    //{
    //    Folder
    //}
}

public class systemData
{
    public string PassWord { get; set; } = "123123";
    public string ImagePath { get; set; } = "e:/images";
    public string SaveImagePath { get; set; } = "e:/images";
    public string MeasurePath { get; set; } = "f:/手动量测";
}

public class MachineData
{
    public int ID { get; set; } = 1;
}